<template>
  <div class="manage">
    <div class="common_form_search">
      <el-form
        ref="searchForm"
        :inline="true"
        :model="searchForm"
      >
        <el-form-item label="商品名称">
          <el-input
            v-model="searchForm.title"
            placeholder="请输入商品名称"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            size="small"
            type="primary"
            @click="search()"
          >搜索</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="flex align-center table-top-btns">
      <h5 class="flex align-center">
        <svg-icon
          style="margin-right:15px"
          icon-class="list"
          :size="15"
        />数据列表
      </h5>
      <el-button
        size="small"
        class="add-btn"
        icon="el-icon-plus"
        v-if="$hasMethod('#addGoods')"
        @click="openAdd()"
      >添加商品</el-button>
      <p
        class="color-999"
        style="margin-left:22px"
      >共查询<span class="red-font">{{total}}</span>个商品</p>
    </div>
    <el-table
      :data="tableData"
      border
      style="width: 100%"
      empty-text="该分类下暂无商品，请添加店内商品"
    >
      <el-table-column
        align="center"
        label="排序"
        width="80"
      >
        <template slot-scope="scope">
          <span v-if="!$hasMethod('#sortGoods')">{{scope.row.sort}}</span>
          <el-input
            v-else
            oninput="value=value.replace(/[^\d]/g,'')"
            maxLength='9'
            v-model="scope.row.sort"
            @change="changeSort(scope.row.id, scope.row.sort)"
          ></el-input>
        </template>
      </el-table-column>
      <el-table-column
        prop="id"
        label="ID"
        width="70"
        align="center"
      />
      <el-table-column
        label="图片"
        width="90"
        align="center"
      >
        <template slot-scope="scope">
          <el-image
            style="width:50px; height:50px"
            :src="scope.row.imgurl?$globalObj.imgBaseUrl+scope.row.imgurl:''"
          >
            <div
              slot="error"
              class="image-slot"
            >
              <i class="el-icon-picture-outline"></i>
            </div>
          </el-image>
        </template>
      </el-table-column>
      <el-table-column
        prop="title"
        label="商品名称"
        align="center"
      />
      <el-table-column
        prop="cat_name"
        label="商品分类"
        align="center"
      />
      <el-table-column
        prop="create_time"
        label="添加时间"
        align="center"
      />
      <el-table-column
        prop="status"
        label="状态"
        align="center"
      >
        <template slot-scope="scope">
          <el-tag :type="scope.row.status == 1 ? 'success' : 'danger'">{{ scope.row.status == 1 ? "已上架" : "已下架" }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        align="center"
        label="操作"
        width="200"
      >
        <template slot-scope="scope">
          <el-button
            v-if="$hasMethod('#deleteGood')"
            type="danger"
            size="mini"
            @click="removeItem(scope.row.id)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <custom-page
      ref="customPage"
      :total="total"
      @getList="getList"
    ></custom-page>
    <goods-dialog
      ref="goodsDialog"
      :multiple="true"
      :show-checked-tool="true"
      :show-cate="false"
      :append-to-body="true"
      @setSelect="setSelect"
    ></goods-dialog>
  </div>
</template>

<script>
import customPage from "@/components/customPage";
import goodsDialog from "@/components/goodsDialog";
import { shopGoodsList, delShopGoods, editSort, addShopGoods } from "@/api/shop";
export default {
  components: { customPage, goodsDialog },
  props: ['editId', 'editName'],
  data() {
    return {
      searchForm: {
        title: ''
      },
      tableData: [],
      total: 0
    }
  },
  methods: {
    // 设置选中的商品
    setSelect(goodsList) {
      const goodsidStr = goodsList.map(k => k.id).join(',');
      addShopGoods({
        cat_id: this.editId,
        goodsidStr
      }).then(res => {
        this.getList();
      })
    },
    // 更改排序
    changeSort(id, val) {
      editSort({
        type: 'ShopGoods',
        id: id,
        sort: val
      }).then(res => {
        this.getList();
      })
    },
    // 删除
    removeItem(id) {
      this.$confirm('确认删除该分类的商品吗？', '删除', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
      }).then(() => {
        delShopGoods(id).then(res => {
          this.getList();
        });
      }).catch(() => { });
    },
    openAdd() {
      // 打开商品选择对话框
      this.$refs.goodsDialog.dialogVisible = true;
    },
    search() {
      this.$refs.customPage.page = 1;
      this.getList();
    },
    getList() {
      const { page, pageSize } = this.$refs.customPage;
      shopGoodsList({
        page,
        pageSize,
        id: this.editId,
        ...this.searchForm
      }).then(res => {
        this.tableData = res.data || [];
        this.total = res.total;
      })
    }
  },
  mounted() {
    this.$route.meta.title = '关联商品 > "' + this.editName + '"分类商品';
    this.getList();
  }
}
</script>

<style>
</style>